package com.henu.engineer.controller.student

import com.alibaba.fastjson.JSONObject
import com.henu.engineer.service.LoginService
import com.henu.engineer.service.AchieveDegreeService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import org.thymeleaf.util.StringUtils

/**
 *
 * @author noexcept
 * @since 2021/8/11 17:35
 */
@RestController
@RequestMapping("/st")
class AchieveDegreeController {

    @Autowired
    lateinit var achieveDegreeService: AchieveDegreeService

    @Autowired
    lateinit var loginService: LoginService

    /**
     * 学生整个四年的毕业达成度，结果为0的话，说明还未生成
     */
    @GetMapping("/graduationAchieved")
    fun graduationAchieved(): String {
        val res = JSONObject()
        if (loginService.currentStudent == null) {
            res["code"] = 0
            res["msg"] = "NOT_LOGGED"
            res["data"] = null
            return res.toJSONString()
        }
        res["code"] = 1
        res["msg"] = "SUCCESS"
        res["data"] = achieveDegreeService.getStudentGraduationAchieved(loginService.currentStudent!!.id!!)
        return res.toJSONString()
    }

    /**
     * 年度毕业要求
     */
    @GetMapping("/periodicalGraduationAchieved")
    fun periodicalGraduationAchieved(): Double {
        TODO()
    }

    /**
     * 某个学生所有课程的达成度
     */
    @GetMapping("/courseAchieved")
    fun studentCourseAchieved(): String {
        val res = JSONObject()
        if (loginService.currentStudent == null) {
            res["code"] = 0
            res["msg"] = "NOT_LOGGED"
            res["data"] = null
            return res.toJSONString()
        }
        res["code"] = 1
        res["msg"] = "SUCCESS"
        res["data"] = achieveDegreeService.getStudentCourseAchieved(loginService.currentStudent!!.id!!)
        return res.toJSONString()
    }

    /**
     * 查询某学生某课程的所有目标达成度
     */
    @GetMapping("/courseGoalAchieved")
    fun courseGoalAchieved(courseId: String?): String {
        val res = JSONObject()
        res["code"] = 0
        res["data"] = null
        if (loginService.currentStudent == null) {
            res["msg"] = "NOT_LOGGED"
            return res.toJSONString()
        }
        if (StringUtils.isEmpty(courseId)) {
            res["msg"]="BLANK_PARAMETER"
            return res.toJSONString()
        }
        res["code"] = 1
        res["msg"] = "SUCCESS"
        res["data"] = achieveDegreeService.getCourseGoalAchieved(loginService.currentStudent!!.id, courseId!!)
        return res.toJSONString()
    }
}